package com.haikesi.api.deviceposition.controller;


import com.github.pagehelper.PageInfo;
import com.haikesi.api.base.ApiIndex;
import com.haikesi.api.base.R;
import com.haikesi.api.base.Type;
import com.haikesi.api.deviceposition.domain.vo.*;
import com.haikesi.api.deviceposition.service.IDevicePositionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import com.haikesi.api.base.Error;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: DevicePositionController
 * @program: haikesi-platform-apis
 * @Author: TanJun
 * @Date: 2019/5/14 14:17
 * @Description: 树形位置管理增删改查
 * @Version: 1.0
 * @Copyright: 2019 重庆海克斯科技有限公司. 注意：本内容仅限于重庆海克斯科技有限公司内部传阅，禁止外泄以及用于其他的商业目
 */

@Api(description = "位置管理相关接口", tags = ApiIndex.DevicePositionController)
@RequestMapping(value = "/api/devicess")
@RestController
public class DevicePositionController {

    @Autowired
    private IDevicePositionService service;

    @ApiOperation(value = "查找位置")
    @GetMapping(value = "/list")
    /*@ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token*/
    public R<PageInfo<List<DevicePositionVO>>> list(
            @ApiParam(value = "查询参数") @ModelAttribute DevicePositionSearchVO searchVO) {
        List<DevicePositionVO> list = service.getList(searchVO);
        PageInfo pageInfo = new PageInfo(list);
        return new R(pageInfo);
    }

    @ApiOperation(value = "查找位置树形")
    @GetMapping(value = "/treeList")
    /*@ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token*/
    public R<List<DevicePositionTreeVO>> getTreeList(
            @ApiParam(value = "查询参数") @ModelAttribute DevicePositionTreeSearchVO searchVO) {
        List<DevicePositionTreeVO> list = service.getTreeList(searchVO);
        return new R(list);
    }

    @ApiOperation(value = "创建位置")
    @PostMapping(value = "")
    public R create(
            @ApiParam(value = "添加参数") @Valid @RequestBody DevicePositionSaveVO seveVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        return new R().setData(service.create(seveVO));
    }

    @ApiOperation(value = "修改位置")
    @PutMapping(value = "")
    /*@ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token*/
    public R update(
            @ApiParam(value = "修改参数") @Valid @RequestBody DevicePositionUpdateVO updateVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        service.update(updateVO);
        return new R();
    }

    @ApiOperation(value = "删除位置")
    @DeleteMapping(value = "")
    /*@ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token*/
    public R delete(
            @ApiParam(value = "修改参数") @RequestParam String id) {
        service.delete(id);
        return new R();
    }
}
